/*
 * ShotMemoryStore.cpp
 *
 *  Created on: Oct 6, 2012
 *      Author: Administrator
 */

#include <FIo.h>
#include <FSystem.h>
#include "ShotMemoryStore.h"

using namespace Osp::Base;
using namespace Osp::Io;


ShotMemoryStore::ShotMemoryStore() {
	counter = 0;
	rootDirName = new String(L"/Home/Shots");
	rootDir = new Directory();
	result r = rootDir -> Construct(rootDirName->GetPointer());
	AppLog("Store %S constructed:%s\n", rootDirName->GetPointer(), GetErrorMessage(r));
}

ShotMemoryStore::~ShotMemoryStore() {
	// TODO Auto-generated distructor stub
}

Shot*
ShotMemoryStore::GetLastShot(void) {

	result r = E_SUCCESS;

	bool hasFiles = false;
	String* rootName = new String("/Home/Shots");
	Directory* root = new Directory();
	root-> Construct(*rootName);
	DirEnumerator* pDirEnum = rootDir -> ReadN();
	int counter = 0;
	while(pDirEnum->MoveNext() == E_SUCCESS) {
		pDirEnum->GetCurrentDirEntry();
		DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
		AppLog("File name is %S", dirEntry.GetName().GetPointer());
		if (!dirEntry.IsDirectory()) {
			if (!hasFiles) {
				rootName->Append("/");
				rootName->Append(dirEntry.GetName());
				hasFiles = true;
			}
//			break;
			counter++;
		}
	}
	delete pDirEnum;
	AppLog("%d shots stored", counter);

//	AllocatedMemory int Returns the amount of memory used by the application (in bytes).
//	AvailableExternalStorage  long long Returns the available disk size of the external storage (in bytes).
//	AvailableInternalStorage

	int avaMemeory;
	int allMemeory;

	String vMemory(L"AvailableMemory");
	String aMemory(L"AllocatedMemory");

	r = Osp::System::RuntimeInfo::GetValue(vMemory, avaMemeory);
	if(IsFailed(r)) {
	AppLog("Failed to get AvailableMemory");
	}

	r = Osp::System::RuntimeInfo::GetValue(aMemory, allMemeory);
	if(IsFailed(r)) {
	AppLog("Failed to get AvailableMemory");
	}

	// return memory value in bytes
	AppLog("Available Memory: %d", avaMemeory);
	AppLog("Allocated Memory: %d", allMemeory);

	if (!hasFiles) {
		delete root;
		return null;
	}

	File* file = new File();
	r = file->Construct(*rootName, "r");
	AppLog("File %S construct result is %S", rootName->GetPointer(), GetErrorMessage(r));
	FileAttributes* attr = new FileAttributes();
	r = File::GetAttributes(*rootName, *attr);
	AppLog("File attributes result is %S", GetErrorMessage(r));

	delete root;

	ByteBuffer* buffer = new ByteBuffer();
	r = buffer->Construct(attr->GetFileSize());
	AppLog("Buffer result is %S", GetErrorMessage(r));

	file->Read(*buffer);
	file->~File();
	r = File::Remove(*rootName);

	AppLog("File %S deleted %S", rootName->GetPointer(), GetErrorMessage(r));

	buffer->Flip();

	AppLog("Buffer position %d", buffer->GetPosition());
	AppLog("Buffer limit %d", buffer->GetLimit());

	String* name = new String("name");

	AppLog("File size is %d", (int)(attr->GetFileSize()));

	return new Shot(buffer, name, buffer->GetLimit());

//	return null;

}

void
ShotMemoryStore::StoreShot(Osp::Base::ByteBuffer& data) {
	File* file = new File();
	String* fileName = new String(rootDirName->GetPointer());
	fileName->Append("/");
	fileName->Append(counter++);
	fileName->Append(".jpg");
	file->Construct(*fileName, L"w+");
	file->Write(data);
	file->Flush();
	if (GetLastResult()==E_SUCCESS) {
		AppLog("File %S is stored", fileName->GetPointer());
		file -> ~File();
	} else {
		AppLog("File %S failed to store %S", fileName->GetPointer(), GetErrorMessage(GetLastResult()));
	}
	delete file;
	delete fileName;
}

void
ShotMemoryStore::DeleteShot(Osp::Base::String* name) {
	AppLog("Deleting %S", name->GetPointer());
}

